//地图容器
var chart = echarts.init(document.getElementById('main'));
var container = document.getElementById("main")

//34个省、市、自治区的名字拼音映射数组
var provinces = {
    //23个省
    "台湾": "taiwan",
    "河北": "hebei",
    "山西": "shanxi",
    "辽宁": "liaoning",
    "吉林": "jilin",
    "黑龙江": "heilongjiang",
    "江苏": "jiangsu",
    "浙江": "zhejiang",
    "安徽": "anhui",
    "福建": "fujian",
    "江西": "jiangxi",
    "山东": "shandong",
    "河南": "henan",
    "湖北": "hubei",
    "湖南": "hunan",
    "广东": "guangdong",
    "海南": "hainan",
    "四川": "sichuan",
    "贵州": "guizhou",
    "云南": "yunnan",
    "陕西": "shanxi1",
    "甘肃": "gansu",
    "青海": "qinghai",
    //5个自治区
    "新疆": "xinjiang",
    "广西": "guangxi",
    "内蒙古": "neimenggu",
    "宁夏": "ningxia",
    "西藏": "xizang",
    //4个直辖市
    "北京": "beijing",
    "天津": "tianjin",
    "上海": "shanghai",
    "重庆": "chongqing",
    //2个特别行政区
    "香港": "xianggang",
    "澳门": "aomen"
};
//直辖市和特别行政区-只有二级地图，没有三级地图
var special = ["北京","天津","上海","重庆","香港","澳门"];
var mapdata = [];
//绘制全国地图
let cc = ArticleUrl;
$.getJSON('/js/china.json', function(data){
    var  d = [];
        axios({
            method: "post",
            url: cc + "policy/indexAreaConut",
            data: {
                data: {
                    type: 1
                }
            }
        }).then((res) => {
            if (res.data.info.status === 200) {
               d = res.data.data;
                mapdata = d;
                //注册地图
                echarts.registerMap('china', data);
                //绘制地图
                renderMap('china',d);
            } else {
                console.info("未查询到数据");
            }
        });
});

container.onclick = function() {        //点击非地图区域地图展示为全国地图
    renderMap('china', mapdata)
}

//地图点击事件
chart.on('click', function (params) {
	if(params.name in provinces ){
		//如果点击的是34个省、市、自治区，绘制选中地区的二级地图
		$.getJSON('/js/province/'+ provinces[params.name] +'.json', function(data){
			echarts.registerMap( params.name, data);
            var d = [];
            if(params.data.areaCode ==undefined){
                for( var i=0;i<data.features.length;i++ ) {
                    d.push({
                        name:data.features[i].properties.name
                    })
                }
                renderMap(params.name,d)
            }else {
                var resData ={areaCode:params.data.areaCode};
                if (special.indexOf( params.name) >=0){//直辖市和特别行政区
                    resData.type = 2;
                }else {//有二级城市的普通省份
                    resData.type = 3;
                }
                axios({
                    method: "post",
                    url: cc + "policy/indexAreaConut",
                    data: {
                        data: resData
                    }
                }).then((res) => {
                    if (res.data.info.status === 200) {
                        var e =[];
                        d = res.data.data;
                       // mapdata = d;
                        for( var i=0;i<data.features.length;i++ ) {
                            e.push({
                                name:data.features[i].properties.name
                            })
                        }
                        e.forEach(E=>{
                            d.forEach(D=>{
                                if (E.name == D.name){
                                    E.value = D.value;
                                    E.areaCode = D.areaCode;
                                }
                            })
                        });
                        renderMap(params.name,e);
                    } else {
                        console.info("未查询到数据");
                    }
                });
            }
		});
    } else if( params.seriesName in provinces ){
		//如果是【直辖市/特别行政区】只有二级下钻
		if(special.indexOf( params.seriesName ) >=0){
		    //跳转政策大全
            //renderMap('china',mapdata);
            if (params.data.areaCode ==undefined){
                window.location.href ='/policy/policy_list';

            }else {
                var data = params.data.areaCode;
                window.location.href ='/policy/policy_list';
                localStorage.setItem("policy_index_search_map", '{"type":"area","areaCode":"'+data+'"}');
            }

        } else {
			//显示县级地图
			$.getJSON('/js/city/'+ cityMap[params.name] +'.json', function(data){
				echarts.registerMap( params.name, data);
                var d = [];
                if (params.data.areaCode ==undefined){
                    for( var i=0;i<data.features.length;i++ ) {
                        d.push({
                            name:data.features[i].properties.name
                        })
                    }
                    renderMap(params.name,d)
                }else {
                    axios({
                        method: "post",
                        url: cc + "policy/indexAreaConut",
                        data: {
                            data:{areaCode:params.data.areaCode,type:4}
                        }
                    }).then((res) => {
                        if (res.data.info.status === 200) {
                            d = res.data.data;
                          //  mapdata = d;

                            var e =[];
                            for( var i=0;i<data.features.length;i++ ) {
                                e.push({
                                    name:data.features[i].properties.name
                                })
                            }
                            e.forEach(E=>{
                                d.forEach(D=>{
                                    if (E.name == D.name){
                                        E.value = D.value;
                                        E.areaCode = D.areaCode;
                                    }
                                })
                            });
                            renderMap(params.name,e);
                        } else {
                            console.info("未查询到数据");
                        }
                    });
                }
			});	
		}	
    } else {
	    //点击区域跳转到政策大全
	   // renderMap('china', mapdata)
        if (params.data.areaCode ==undefined){
            window.location.href ='/policy/policy_list';

        }else {
            var data = params.data.areaCode;
            window.location.href ='/policy/policy_list';
            localStorage.setItem("policy_index_search_map", '{"type":"area","areaCode":"'+data+'"}');
        }
	}
});

//初始化绘制全国地图配置
var option = {
    title : {
        text: '',
        left: 'center',
        textStyle:{
            color: '#fff',
            fontSize:24,
            fontWeight:'bold',
            fontFamily:"Microsoft YaHei"
        },
    },
    tooltip: {
        trigger: 'item',
        //formatter: '{a}'   //设置tip显示的内容
        formatter: function(params) {
            return params.name + "："+ (isNaN(params.value)?0:params.value)+"条";
        }
    },
    animationDuration:1000,
	animationEasing:'cubicOut',
	animationDurationUpdate:1000
     
};
function renderMap(map,data){
    option.series = [ 
		{
            name: map,
            type: 'map',
            mapType: map,
            roam: false,
            nameMap:{
			    'china':'中国'
			},
            label: {
	            normal:{
					show: true,    //地区文字是否显示
					textStyle:{
						color:'#fff',
						fontSize:14
					}  
	            },
	            emphasis: {
	                show: true,  //鼠标移上去地区文字是否显示
	                textStyle:{
						color:'#fff',
						fontSize:14
					}
	            }
	        },
	        itemStyle: {
	            normal: {
	                areaColor: 'transparent',    //地图区域颜色
	                borderColor: '#3fdaff'   //地图边线的颜色
	            },
	            emphasis: {
	                areaColor: '#515C9E',
                    borderColor: '#ffffff',
	            }
	        },
            data:data
        }	
    ];
    if(map == "海南") {
        option.series[0].label.normal.show = false
    }
    //渲染地图
    chart.setOption(option)
}